/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙，邂逅框架梦]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_manager.htm
 *
 * Zhiqim Manager is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager.action;

import java.util.ArrayList;
import java.util.List;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.extend.GetPostAction;
import org.zhiqim.httpd.validate.ones.IsNotEmpty;
import org.zhiqim.kernel.json.Jsons;
import org.zhiqim.kernel.tree.Tree;
import org.zhiqim.kernel.tree.TreeBuilder;
import org.zhiqim.manager.dao.ZmrOperatorDao;
import org.zhiqim.manager.dbo.ZmrMenu;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.manager.dbo.ZmrOperatorRule;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;

/**
 * 修改操作员/管理员权限页面
 *
 * @version v1.0.0 @author zouzhigang 2015-5-28 新建与整理
 */
public class OperatorRuleAction extends GetPostAction
{
    protected void validate(HttpRequest request)
    {
        request.addValidate(new IsNotEmpty("operatorCode", "请选择操作员"));
    }

    @Override
    protected void doGet(HttpRequest request) throws Exception
    {
        request.setResponsePrivateCache();
        
        String operatorCode = request.getParameter("operatorCode");
        ZmrOperator operator = ORM.table().item(ZmrOperator.class, operatorCode);
        if (operator == null)
        {
            request.returnHistory("您选择的操作员不存在");
            return;
        }

        // 获取操作员权限
        List<ZmrOperatorRule> ruleList = ORM.table().list(ZmrOperatorRule.class, new Selector("operatorCode", operatorCode));
        
        //获取能管理的权限
        List<ZmrMenu> list = ZmrOperatorDao.getAllMenuList(request);
        if (list.isEmpty())
        {
            request.setRedirect("/manager/operator.htm", "系统未设置任何权限");
            return;
        }
        
        Tree<ZmrMenu> tree = TreeBuilder.newTree(list, "parentCode", "menuCode");

        request.setAttribute("operator", operator);
        request.setAttribute("menuSize", list.size());
        request.setAttribute("tree", tree);
        request.setAttribute("ruleList", ruleList);
    }
    
    @Override
    protected void doPost(HttpRequest request) throws Exception
    {
        String operatorCode = request.getParameter("operatorCode");
        String[] itemCodes = request.getParameterValues("rule");
        if (itemCodes == null)
            itemCodes = new String[0];
    
        // 判断操作员是否存在
        if (ORM.table().count(ZmrOperator.class, operatorCode) == 0)
        {
            request.returnHistory("您选择的操作员不存在");
            return;
        }
    
        // 重置操作员权限
        ORM.table().delete(ZmrOperatorRule.class, new Selector("operatorCode", operatorCode));
        if (itemCodes.length > 0)
        {
            List<ZmrOperatorRule> bathList = new ArrayList<ZmrOperatorRule>();
            for (int i = 0; i < itemCodes.length; i++)
            {
                ZmrOperatorRule rule = new ZmrOperatorRule();
                rule.setOperatorCode(operatorCode);
                rule.setMenuCode(itemCodes[i]);
                
                bathList.add(rule);
            }

            ORM.table().insertBatch(bathList);
        }
        
        request.setAlertMsg("保存成功");
        request.addParam("operatorCode", operatorCode);
        
        //增加操作日志
        ZmrOperatorDao.addOperateLog(request, "修改操作员独立权限", Jsons.toString("operatorCode", operatorCode));
    }
}
